package com.r2coding.security;

import com.r2coding.filter.JwtAuthenticationTokenFilter;
import com.r2coding.security.mobile.MobileAuthenticationConfig;
import com.r2coding.security.mobile.MobileValidateFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * SpringSecurity配置类
 *
 * @author: 98k灬
 * @since: 2022/10/31
 * @email: 98k_style@sina.com
 * @公众号: 爪哇Guy灬
 **/
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    CustomUserDetailService customUserDetailService;

    @Resource
    CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;

    @Resource
    CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Resource
    CustomLogoutSuccessHandler customLogoutSuccessHandler;

    @Resource
    CustomAuthenticationEntryPoint customAuthenticationEntryPoint;

    @Resource
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Resource
    MobileValidateFilter mobileValidateFilter;

    @Resource
    MobileAuthenticationConfig mobileAuthenticationConfig;

    public static final String[] URL_WHITELISTS = {
            "/user/login", "/mobile/login", "/mobile/code"
    };

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailService);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        // 自定义登录接口需要用到
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //.antMatchers("/user/login", "/mobile/login", "/mobile/code").anonymous()
                .antMatchers(URL_WHITELISTS).permitAll()
                .anyRequest().authenticated()
                .and()
                .exceptionHandling().authenticationEntryPoint(customAuthenticationEntryPoint);

        // 配置JWT过滤器
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterBefore(mobileValidateFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterAfter(mobileValidateFilter, JwtAuthenticationTokenFilter.class);

        http.formLogin().successHandler(customAuthenticationSuccessHandler)
                .failureHandler(customAuthenticationFailureHandler);

        http.apply(mobileAuthenticationConfig);
    }
}
